package com.jd.pet.service.impl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.jd.basic.util.BaiduAuditUtils;
import com.jd.basic.util.DistanceUtil;
import com.jd.basic.util.LoginContext;
import com.jd.basic.util.Point;
import com.jd.org.domain.Shop;
import com.jd.org.mapper.ShopMapper;
import com.jd.pet.domain.SearchMasterMsg;
import com.jd.pet.domain.SearchMasterMsgAuditLog;
import com.jd.pet.dto.SearchMasterMsgDto;
import com.jd.pet.mapper.SearchMasterMsgAuditLogMapper;
import com.jd.pet.mapper.SearchMasterMsgMapper;
import com.jd.pet.service.ISearchMasterMsgAuditLogService;
import com.jd.pet.service.ISearchMasterMsgService;
import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.user.domain.Logininfo;
import com.jd.user.domain.User;
import com.jd.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shaqian
 * @since 2023-03-16
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper msgMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper logMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    @Transactional
    public void publish(SearchMasterMsgDto serrchMasterMsgDto) {
        // 创建审核日志对象
        SearchMasterMsgAuditLog msgAuditLog = new SearchMasterMsgAuditLog();
        // 寻主对象
        SearchMasterMsg searchMasterMsg = new SearchMasterMsg();
        Logininfo logininfo = LoginContext.getLogininfo();
        String phone = logininfo.getPhone();
        String email = logininfo.getEmail();
        if (phone!=null){
            User user = userMapper.findByPhone(phone);
            searchMasterMsg.setUserId(user.getId());
        }
        if (email!=null){
            User user = userMapper.findByEmail(email);
            searchMasterMsg.setUserId(user.getId());
        }
        BeanUtils.copyProperties(serrchMasterMsgDto,searchMasterMsg);
        // 参数校验
        // 先判断位置 50公里以内
        Point custmor = DistanceUtil.getPoint(serrchMasterMsgDto.getAddress());
        List<Shop> shops = shopMapper.findAll();
        Shop nearestShop = DistanceUtil.getNearestShop(custmor, shops);
        if (nearestShop != null){
            searchMasterMsg.setShopId(nearestShop.getId());
        }
        msgMapper.add(searchMasterMsg);
        // 百度Ai审核
        if (BaiduAuditUtils.TextCensor(serrchMasterMsgDto.getName())){
            msgAuditLog.setNote("审核通过");

        }else {
            msgAuditLog.setNote("名称违规！");
        }
        msgAuditLog.setMsgId(searchMasterMsg.getId());
        msgAuditLog.setAuditId(1L);
        msgAuditLog.setAuditTime(new Date());
        msgAuditLog.setState(1);
        logMapper.add(msgAuditLog);
    }
}
